﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace MabiPatch
{
	/// <summary>
	/// Interaction logic for UI.xaml
	/// </summary>
	public partial class PatcherUI : UserControl
	{
        private Patcher _patcher;
        
        private void runWaitThread(System.Threading.Thread t)
        {
        	t.Start();
        	while (t.IsAlive)
        		System.Windows.Forms.Application.DoEvents();
        }
		public PatcherUI(Patcher Patch)
		{
            Patch.updatePatchNumber = new Action(UpdateVersDisplay);
            _patcher = Patch;
			InitializeComponent();
		}

       
        bool vRead = false;
        Label vLabel;
        private void OnLoad(object sender, RoutedEventArgs e)
        {
        	if (vRead)
        		return;

            Properties.Settings.Default.Reload();
            delParts.IsChecked = Properties.Settings.Default.DeleteParts;
            delTree.IsChecked = Properties.Settings.Default.DeleteContent;
            delZIP.IsChecked = Properties.Settings.Default.DeleteZIPs;
            allowPrepatch.IsChecked = Properties.Settings.Default.AllowPrepatch;

        	List<int> v = Common.GetAvailableVersions();
        	foreach (int ver in v)
        		versionCombo.Items.Add(ver);
            versionCombo.SelectedIndex = 0;
        	Window w = GetTopParent();
        	vLabel = (Label)w.FindName("ClientVersLabel");
        	UpdateVersDisplay();
            vRead = true;
        }
       
       private Window GetTopParent()
       {
           DependencyObject dpParent = this.Parent;
           do
           {
               dpParent = LogicalTreeHelper.GetParent(dpParent);
           } while (dpParent.GetType().BaseType != typeof(Window));
           return dpParent as Window;
       }
       
       internal void UpdateVersDisplay()
       {
    	vLabel.Dispatcher.Invoke(new Action<string>((s) =>
                                    {
                                    	vLabel.Content = s;
                                    }), Common.ReadVersion().ToString());

       }

       private void redlLang_Click(object sender, RoutedEventArgs e)
       {
       	runWaitThread(new System.Threading.Thread((() => _patcher.RedownloadLanFile())));
       }

       private void writeCustomNumber_Click(object sender, RoutedEventArgs e)
       {
           int vers;
           bool success;
           do
           {
               string t = InputBox.Show("Enter new version number", "Enter number", _patcher.OPI.LocalVersion.ToString(), -1, -1);
               if (t == "")
                   return;
               success = int.TryParse(t, out vers);
           } while (!success);
           Common.WriteVersion(vers);
           UpdateVersDisplay();
       }

       private void patchTo_Click(object sender, RoutedEventArgs e)
       {
           int cVers = Common.ReadVersion();
           int tVers = (int)versionCombo.SelectedItem;
           if (tVers < cVers)
           	runWaitThread(new System.Threading.Thread(() => _patcher.DownPatch(tVers)));
           else
           	runWaitThread(new System.Threading.Thread(() => _patcher.Patch(tVers, cVers)));
       }

       private void redlCV_Click(object sender, RoutedEventArgs e)
       {
       	runWaitThread(new System.Threading.Thread(() => _patcher.Patch(Common.ReadVersion() + "_full")));
       }

       private void checkbox_Checked(object sender, RoutedEventArgs e)
       {
           if (!vRead)
               return;
           Properties.Settings.Default.AllowPrepatch = (bool)allowPrepatch.IsChecked;
           Properties.Settings.Default.DeleteZIPs = (bool)delZIP.IsChecked;
           Properties.Settings.Default.DeleteParts = (bool)delParts.IsChecked;
           Properties.Settings.Default.DeleteContent = (bool)delTree.IsChecked;
           Properties.Settings.Default.Save();
       }
	}
}